let express = require("express");
let qs = require("querystring");
let comment = express.Router();
let { pool } = require("../db/pool");
comment.post('/comment/submit',(req,res)=>{
    // console.log(req.query)
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let comment_id = Math.random()*10000;
        let time = Date.now();
        let sql = `select user_id from base_user where user_name='${req.body.username}'`;
        connection.query(sql,(err,result)=>{
            let user_id = result[0].user_id;
            let sql = `insert into comment_tb (comment_id,comment_content,dy_id,user_id,comment_time) 
            values ('${comment_id}','${req.body.content}','${req.body.dy_id}','${user_id}','${time}')`
            connection.query(sql,(err,result)=>{
                if(err) throw err;
                // console.log(result)
                res.send(result)
                connection.release()
        })
        })
        
    })
})
comment.get('/comment/MyComment',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select user_id from base_user where user_name='${req.query.username}'`;
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            let sql = `select * from movie_comment where user_id='${result[0].user_id}'`
            connection.query(sql,(err,result)=>{
                if(err) throw err;
                res.send(result);
            })
        })
        connection.release();
    })
})
module.exports = {
    comment
}